#include "weatherlable.h"

extern QString code;

weatherLable::weatherLable(QWidget *parent) : QLabel(parent)
{
    manager = new QNetworkAccessManager(this);
    connect(manager, &QNetworkAccessManager::finished,
            this, &weatherLable::replyFinished);
    qDebug()<<::code;
    manager->get(QNetworkRequest(QUrl(QString("http://t.weather.sojson.com/api/weather/city/%1").arg(::code))));
}

weatherLable::~weatherLable()
{

}

void weatherLable::paintEvent(QPaintEvent *pe)
{
    QPainter painter(this);
    painter.fillRect(this->rect(),QBrush(QColor(50,105,183)));

    painter.setRenderHints(QPainter::Antialiasing, true);
    //使用平滑的pixmap变换算法(双线性插值算法)
    painter.setRenderHint(QPainter::SmoothPixmapTransform, true);

    QString str = parent + " " + city;
    painter.setPen(Qt::white);
    QFont f("SimHei", 10);
    QFontMetrics m(f);
    painter.setFont(f);
    int w = this->rect().width() - m.size(Qt::TextSingleLine, str).width();
    painter.drawText(w, 25, str);

    painter.setFont(QFont("SimHei", 12, QFont::Bold));
    painter.drawText(10, 25, QString("当前天气"));

    painter.setFont(QFont("MingLiU", 9));
    painter.drawText(10, 45, time);

    painter.setFont(QFont("Microsoft YaHei", 20, QFont::Bold));
    painter.drawText(220, 115, type);
    painter.drawText(220, 155, wendu + "℃");
    painter.drawText(220, 195, shidu);

    QFont font1("SimHei", 12, QFont::Bold);
    painter.setFont(font1);
    painter.drawText((this->rect().width() - QFontMetrics(font1).size(Qt::TextSingleLine, "!!!" + notice).width()) / 2,
                     255, "!!!" + notice);

    QFont font2("Microsoft YaHei", 10);
    painter.setFont(font2);
    QFontMetrics metrics(font2);

    int widthQ = metrics.size(Qt::TextSingleLine, "空气质量").width();
    int widthF = metrics.size(Qt::TextSingleLine, "风速").width();
    int width = (this->rect().width() - widthQ * 3 - widthF) / 2;

    painter.drawText(20 * 1 + width * 0, 290, QString("空气质量"));
    painter.drawText(25 * 2 + width * 1, 290, QString("最高气温"));
    painter.drawText(25 * 3 + width * 2, 290, QString("最低气温"));
    painter.drawText(30 * 4 + width * 3, 290, QString("风速"));

    painter.drawText(20 * 1 + width * 0 + (widthQ - metrics.size(Qt::TextSingleLine, quality).width()) / 2, 310, quality);
    painter.drawText(25 * 2 + width * 1 + (widthQ - metrics.size(Qt::TextSingleLine, high).width()) / 2, 310, high);
    painter.drawText(25 * 3 + width * 2 + (widthQ - metrics.size(Qt::TextSingleLine, low).width()) / 2, 310, low);
    painter.drawText(30 * 4 + width * 3 + (widthF - metrics.size(Qt::TextSingleLine, fx + ' ' + fl).width()) / 2, 310, fx + ' ' + fl);


    if("晴" == type)
    {
        QStringList now = time.split(':');
        QStringList rise = sunrise.split(':');
        QStringList set = sunset.split(':');
        qDebug()<<rise[0].toInt()<<now[0].toInt()<<rise[1].toInt()<<now[1].toInt()<<
            set[0].toInt()<<now[0].toInt()<<set[1].toInt()<<now[1].toInt();
        if((rise[0].toInt() < now[0].toInt() && set[0].toInt() > now[0].toInt()) ||
           ((rise[0].toInt() == now[0].toInt() && rise[0].toInt() <= now[0].toInt()) ||
            (set[0].toInt() == now[0].toInt() && set[0].toInt() >= now[0].toInt())))
        {
            painter.drawPixmap(15,50,175,175,QPixmap("太阳.png"));
        }
        else
        {
            painter.drawPixmap(15,50,175,175,QPixmap("月亮.png"));
        }
    }
    else if("阴" == type)
    {
        painter.drawPixmap(15,50,175,175,QPixmap("阴天.png"));
    }
    else if("多云" == type)
    {
        painter.drawPixmap(15,50,175,175,QPixmap("多云.png"));
    }
    else if("霾" == type)
    {
        painter.drawPixmap(15,60,160,160,QPixmap("多雾.png"));
    }
    else if(true == type.contains("雨"))
    {
        painter.drawPixmap(15,50,175,175,QPixmap("下雨.png"));
    }
    else if(true == type.contains("雪"))
    {
        painter.drawPixmap(15,50,175,175,QPixmap("下雪.png"));
    }
}

void weatherLable::analysis(QByteArray byte)
{
    QJsonDocument doc = QJsonDocument::fromJson(byte);
    QJsonObject object;
    QJsonArray array;

    if(true == doc.isObject())
    {
        object = doc.object();
        if(true == object.contains("time") && true == object.value("time").isString())
        {
            time = object.value("time").toString();
        }
        if(true == object.contains("cityInfo") && true == object.value("cityInfo").isObject())
        {
            QJsonObject o = object.value("cityInfo").toObject();
            if(true == o.contains("city") && true == o.value("city").isString())
            {
                city = o.value("city").toString();
            }
            if(true == o.contains("parent") && true == o.value("parent").isString())
            {
                parent = o.value("parent").toString();
            }
        }
        if(true == object.contains("data") && true == object.value("data").isObject())
        {
            QJsonObject o = object.value("data").toObject();
            if(true == o.contains("shidu") && true == o.value("shidu").isString())
                shidu = o.value("shidu").toString();
            if(true == o.contains("quality") && true == o.value("quality").isString())
                quality = o.value("quality").toString();
            if(true == o.contains("wendu") && true == o.value("wendu").isString())
                wendu = o.value("wendu").toString();
            if(true == o.contains("forecast") && true == o.value("forecast").isArray())
            {
                array = o.value("forecast").toArray();
                size = array.size();
                o = array[0].toObject();
                if(true == o.contains("high") && true == o.value("high").isString())
                {
                    high = o.value("high").toString();
                    QStringList list = high.split(' ');
                    high= list[1];
                }
                if(true == o.contains("low") && true == o.value("low").isString())
                {
                    low = o.value("low").toString();
                    QStringList list = low.split(' ');
                    low = list[1];
                }
                if(true == o.contains("ymd") && true == o.value("ymd").isString())
                    ymd = o.value("ymd").toString();
                if(true == o.contains("week") && true == o.value("week").isString())
                    week = o.value("week").toString();
                if(true == o.contains("sunrise") && true == o.value("sunrise").isString())
                    sunrise = o.value("sunrise").toString();
                if(true == o.contains("sunset") && true == o.value("sunset").isString())
                    sunset = o.value("sunset").toString();
                if(true == o.contains("type") && true == o.value("type").isString())
                    type = o.value("type").toString();
                if(true == o.contains("fx") && true == o.value("fx").isString())
                    fx = o.value("fx").toString();
                if(true == o.contains("fl") && true == o.value("fl").isString())
                    fl = o.value("fl").toString();
                if(true == o.contains("notice") && true == o.value("notice").isString())
                    notice = o.value("notice").toString();

                QStringList list = time.split(' ');
                list = list[1].split(':');
                time = list[0]+ ':' + list[1];

                update();
            }
        }
    }

}

void weatherLable::replyFinished(QNetworkReply *reply)
{
    if(200 == reply->attribute(QNetworkRequest::HttpStatusCodeAttribute))
    {
        analysis(reply->readAll());
    }
}

void weatherLable::reRequest(QString code)
{
    manager->get(QNetworkRequest(QUrl(QString("http://t.weather.sojson.com/api/weather/city/%1").arg(code))));
}
